package com.example.sunlight.unicomsensing.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunlight.unicomsensing.R;
import com.example.sunlight.unicomsensing.data.Datas;
import com.example.sunlight.unicomsensing.server.MyAlerDialog;
import com.example.wzsdk.login.Landing;
import com.example.wzsdk.login.LoginState;
import com.example.wzsdk.scan.Search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Created by SunLight on 2017/9/15.
 */

public class CarMainFragment extends Fragment implements View.OnClickListener, MyAlerDialog.OnEditInputFinishedListener, Search.SearchFish, LoginState {
    TextView hum, tai, gpss, max, ya, msgs;
    Button up, down;
    ImageView logo;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    gpss.setText("N" + new Random().nextInt(100) + "°" + "E" + new Random().nextInt(100) + "°");
                    ya.setText(new Random().nextInt(9) + "." + new Random().nextInt(9) + "bar");
                    break;
                case 0x456:
                    search.EngSearch(activity);
                    break;
                case 0x789:
                    hum.setText(num + "℃");
                    break;
                case 0x684:
                    max.setText(car + "m");
                    break;
                case 0x666:
                    msgs.setText(Datas.Msg);
                    break;
                case 0x888:
                    dialog();
                    msgs.setText("");
                    break;
            }
        }
    };

    protected void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage("与主机断开连接！");
        builder.setTitle("系统提示");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(false).create().show();
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.sendEmptyMessage(0x123);
            handler.postDelayed(this, 5 * 1000);
        }
    };
    Activity activity;

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    SharedPreferences sp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_main, container, false);
        sp = activity.getSharedPreferences("text", Context.MODE_PRIVATE);
        FindView(view);
        Onclick();
        handler.postDelayed(runnable, 1000);
        msgs.setText(Datas.Msg);
        list = new ArrayList<>();
        myAlerDialog = new MyAlerDialog(activity, list, this);
        initSDK();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!sp.getString("ip", "null").equals("null")) {
            landing.Login(sp.getString("ip", "null"), 5800);
        } else {
            Toast.makeText(activity, "请配置主机信息！", Toast.LENGTH_LONG).show();
        }
    }

    public void FindView(View view) {
        hum = (TextView) view.findViewById(R.id.hum);
        tai = (TextView) view.findViewById(R.id.tai);
        gpss = (TextView) view.findViewById(R.id.gpss);
        max = (TextView) view.findViewById(R.id.max);
        ya = (TextView) view.findViewById(R.id.ya);
        up = (Button) view.findViewById(R.id.up);
        down = (Button) view.findViewById(R.id.down);
        msgs = (TextView) view.findViewById(R.id.msg);
        logo = (ImageView) view.findViewById(R.id.logo);
    }

    public void Onclick() {
        up.setOnClickListener(this);
        down.setOnClickListener(this);
        logo.setOnClickListener(this);
    }

    boolean upstate = false;
    boolean downstate = false;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logo:
                ShowDialog();
                break;
            case R.id.up:
                tai.setText("+15°");
                landing.setKeyVariable("10014",1);
                break;
            case R.id.down:
                tai.setText("0°");
                landing.setKeyVariable("10014",3);
                break;
        }
    }

    public void initSDK() {
        search = new Search("ZIWDHQKGA");
        landing = new Landing("ZIWDHQKGA");
        search.setSearchFish(this);
        landing.setLoginState(this);
    }

    MyAlerDialog myAlerDialog;
    List<String[]> list;
    Search search;
    Landing landing;
    EditText ip, host;

    public void ShowDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.dialog);
        View view = View.inflate(activity, R.layout.alertdialog_item, null);
        builder.setView(view);
        builder.setCancelable(true);
        ip = (EditText) view.findViewById(R.id.ip);
        host = (EditText) view.findViewById(R.id.host);
        Button login = (Button) view.findViewById(R.id.login);
        Button scan = (Button) view.findViewById(R.id.search);

        final AlertDialog dialog = builder.create();
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0x456);
                myAlerDialog.show();
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sp.edit().putString("ip", ip.getText().toString()).commit();
                landing.Login(ip.getText().toString(), 5800);
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void editInputFinished(String ips, String serials, int number) {
        ip.setText(ips);
        host.setText(serials);
    }

    @Override
    public void SearchFishWifi(List<String[]> list) {

    }

    @Override
    public void SearchFishWna(List<String[]> list) {
        if (list != null) {
            myAlerDialog.setData(list);
            myAlerDialog.refurbish();
        }
    }

    @Override
    public void LoginStatus(boolean b, String s) {
        if (b == true) {
            for (int i = 0; i < landing.getDateList().size(); i++) {
                System.out.println("awdasdad  " + Arrays.toString(landing.getDateList().get(i)));
            }
            Datas.Msg = s;
            landing.atnVariable(Datas.Hum);
            landing.atnVariable(Datas.Car);
            handler.sendEmptyMessage(0x666);
        } else {
            if (sp.getString("ip", "null").equals("null")) {
                handler.sendEmptyMessage(0x888);
            }
        }
    }

    int num;
    int car;

    @Override
    public void AttentionStarting(String uuid, byte number, String index, String porid, String host) {
        switch (uuid) {
            case Datas.Hum:
                if (index.equals("1")) {
                    num = number;
                    handler.sendEmptyMessage(0x789);
                }
                break;
            case Datas.Car:
                car = number & 0xff;
                handler.sendEmptyMessage(0x684);
                break;
        }
    }

    @Override
    public void AttentionProcess(String uuid, byte number, String text, String index, String proid, String host) {
        switch (uuid) {
            case Datas.Hum:
                if (index.equals("1")) {
                    num = number;
                    handler.sendEmptyMessage(0x789);
                }
                break;
            case Datas.Car:
                car = number & 0xff;
                handler.sendEmptyMessage(0x684);
                break;
        }
    }

    @Override
    public void SetVarState(String s, byte b, String s1) {

    }

    @Override
    public void SetKeyState(String s, String s1, String s2) {

    }


}
